﻿using System;
using System.Windows.Forms;

namespace Calculator
{
    public partial class formMian : Form
    {
        double num1 = 0;
        double result = 0;
        char op;
        bool needClear = false;
        bool isFirst = false;  // 标记连续单步运算的起点
        double getTextBoxResult() {
            try {                        // 避免读取到""或"除数不能为0"
                return double.Parse(textBoxResult.Text.Trim());
            } catch (FormatException ex) {
                return 0;
            }
        }

        void computeResult()
        {
            result = getTextBoxResult();
            switch (op)
            {
                case '+':
                    result = num1 + result; break;
                case '-':
                    result = num1 - result; break;
                case '*':
                    result = num1 * result; break;
                case '/':  // double类型的数除以 0 所得的结果是Infinity(无穷大), 而 0.0/0.0 = NaN (Not a Number), 并不会发生异常
                    result = num1 / result;
                    break;
                case '%':
                    result = num1 % result;
                    break;
            }
            if (Double.IsInfinity(result) || Double.IsNaN(result))
            {
                textBoxResult.Text = "除数不能为0";
            }
            else
                textBoxResult.Text = result.ToString();
            needClear = true;
        }

        public formMian()
        {
            InitializeComponent();
        }
        private void form_Load(object sender, EventArgs e) 
        {
            textBoxResult.Text = "0";
            needClear = true;
            isFirst = true;
        }
        private void btnNum_Click(object sender, EventArgs e) // 0 1 2 3 4 5 6 7 8 9 .
        {
            if (needClear == true) {
                textBoxResult.Text = "";
                needClear = false;
            }
            string btnText = ((Button)sender).Text;
            if (btnText!="." || textBoxResult.Text.IndexOf(".") < 0)  // 避免重复出现小数点
                textBoxResult.Text += btnText;
        }

        private void btnOperate_Click(object sender, EventArgs e)  // + - * / %
        {
            if (isFirst) {
                needClear = true;
                isFirst = false;
            } else {
                computeResult();
            }
            op = char.Parse(((Button)sender).Text);
            num1 = getTextBoxResult();
            
        }

        private void btnReciprocal_Click(object sender, EventArgs e) // 1/x
        {
            result = getTextBoxResult();
            if(result != 0)
                textBoxResult.Text = (1/result).ToString();
        }

        private void btnSqrt_Click(object sender, EventArgs e) // √
        {
            result = getTextBoxResult();
            textBoxResult.Text = Math.Sqrt(result).ToString();
        }

        private void btnOpposite_Click(object sender, EventArgs e) // ±
        {
            result = getTextBoxResult();
            textBoxResult.Text =(-result).ToString();
        }

        private void btnEmpty_Click(object sender, EventArgs e) // C：清除所有的数
        {
            num1 = 0;
            textBoxResult.Text = "";
            isFirst = true;
        }

        private void btnDel_Click(object sender, EventArgs e) // CE：清除当前输入的数,
        {
            textBoxResult.Text = "";
        }

        private void btnBackspace_Click(object sender, EventArgs e)  // ←：退格
        {
            if(textBoxResult.Text.Length>0)
                textBoxResult.Text = textBoxResult.Text.Remove(textBoxResult.Text.Length - 1);
        }

        private void btnEqual_Click(object sender, EventArgs e) // =
        {
            computeResult();
            isFirst = true;
        }
    }
}
